package com.oos.zone.wechat.mp.config;

import com.oos.zone.wechat.mp.service.handler.WxMpScanCodeMessageHandler;
import com.oos.zone.wechat.mp.service.handler.WxMpStandardMessageHandler;
import com.oos.zone.wechat.mp.service.handler.WxMpSubscribeMessageHandler;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 功能描述：
 *
 * @author 拓金辉
 * @version 1.0
 * @date 2021-08-06 22:12:14
 */
@Configuration
public class WxMpServiceConfig {

    @Resource
    private WxMpStandardMessageHandler wxMpStandardMessageHandler;

    @Resource
    private WxMpSubscribeMessageHandler wxMpSubscribeMessageHandler;

    @Resource
    private WxMpScanCodeMessageHandler wxMpScanCodeMessageHandler;

    /**
     * 方法描述：创建 WxMpService
     *
     * @return me.chanjar.weixin.mp.api.WxMpService
     * @author 拓金辉
     * @date 2021-08-08 19:46:44
     */
    @Bean
    public WxMpService wxMpService() {
        final WxMpService wxMpService = new WxMpServiceImpl();

        WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        wxMpConfigStorage.setAppId("wx81e38717cc6d919d");
        wxMpConfigStorage.setSecret("28e2a50d3109cb3a901800eda4f91deb");
        wxMpConfigStorage.setAesKey("7R6ZhP13nyvpdrmuWXaKSNEYTFxzDtzRn14OS7brnZU");
        wxMpConfigStorage.setToken(this._token(wxMpConfigStorage.getAppId(), wxMpConfigStorage.getSecret()));

        wxMpService.addConfigStorage(wxMpConfigStorage.getAppId(), wxMpConfigStorage);
        return wxMpService;
    }

    /**
     * Token生成算法
     */
    private String _token(String appId, String appSecret) {
        return UUID.nameUUIDFromBytes((appId + appSecret).getBytes(StandardCharsets.UTF_8)).toString().replace("-", "");
    }

    /**
     * 方法描述：创建 WxMpMessageRouter
     *
     * @return me.chanjar.weixin.mp.api.WxMpMessageRouter
     * @author 拓金辉
     * @date 2021-08-09 11:25:12
     */
    @Bean
    public WxMpMessageRouter wxMpMessageRouter() {
        final List<String> standardMessages = Arrays.asList("text", "image", "voice", "video", "shortvideo", "location", "link", "file");
        final List<String> subscribes = Arrays.asList("subscribe", "unsubscribe");
        final List<String> scancodes = Arrays.asList("scancode_push", "scancode_waitmsg");

        return new WxMpMessageRouter(wxMpService())
                // 普通消息处理器
                .rule().async(false)
                .event(null)
                .matcher(item -> standardMessages.contains(item.getMsgType()))
                .handler(wxMpStandardMessageHandler)
                .end()
                // 关注/取消关注事件处理器
                .rule().async(false)
                .msgType(WxConsts.XmlMsgType.EVENT)
                .matcher(item -> subscribes.contains(item.getEvent()))
                .handler(wxMpSubscribeMessageHandler)
                .end()
                // 扫码推事件处理器
                .rule().async(false)
                .msgType(WxConsts.XmlMsgType.EVENT)
                .matcher(item -> scancodes.contains(item.getEvent()))
                .handler(wxMpScanCodeMessageHandler)
                .end();
    }

}
